/*
 * @Author: 张笑天
 * @Date: 2018-03-07 17:11:37
 * @Last Modified by: lyuan2018
 * @Last Modified time: 2019-12-23 18:12:46
 */
import { observable, action } from 'mobx';
import { message } from 'antd';
import { WeaTableNew } from 'comsMobx';
import { WeaTools, WeaLocaleProvider } from 'ecCom';

import Api from '../apis/view';
import opsApi from '../apis/ops';

const getLabel = WeaLocaleProvider.getLabel;
const { Base64 } = WeaTools;
const { TableStore } = WeaTableNew;

const initState = {
  loading: false,
  dropdownLoding: false,
  rightLoading: false,
  baseLoading: false,
  contentLoading: false,
  readListLoading: false,
  fastReplyLoading: false,
  right: 0,
  baseInfo: {},
  dropDown: [],
  content: '',
  isSimple: 1,
  readListTableStore: new TableStore(),
  readListCount: [],
  readListBtnSelected: '',
  showQuickReplay: false,
  replycontent: '',
  sendApartResultList: [], // 分别发送结果列表
  isSendApartResultListVis: false, // 是否显示分别发送结果列表
  sendStatus: '0',
  emailRecallDetailList: [], // 撤回结果列表
  isEmailRecallDetailListVis: false, // 是否显示撤回结果列表
  recallStatus: '0',
  recallRule: '',
  recallNoTrace: '',
  classificationRight: 0,
};

export default class EmailViewStore {
  id = '';
  @observable loading = initState.loading;
  @observable dropdownLoding = initState.dropdownLoding;
  @observable rightLoading = initState.rightLoading;
  @observable baseLoading = initState.baseLoading;
  @observable contentLoading = initState.contentLoading;
  @observable readListLoading = initState.readListLoading;
  @observable fastReplyLoading = initState.fastReplyLoading;
  @observable right = initState.right;
  @observable baseInfo = initState.baseInfo;
  @observable dropDown = initState.dropDown;
  @observable content = initState.content;
  @observable isSimple = initState.isSimple;
  @observable readListTableStore = initState.readListTableStore;
  @observable readListCount = initState.readListCount;
  @observable readListBtnSelected = initState.readListBtnSelected;
  @observable showQuickReplay = initState.showQuickReplay;
  @observable replycontent = initState.replycontent;
  @observable sendApartResultList = initState.sendApartResultList;
  @observable isSendApartResultListVis = initState.isSendApartResultListVis;
  @observable sendStatus = initState.sendStatus;
  @observable emailRecallDetailList = initState.emailRecallDetailList;
  @observable isEmailRecallDetailListVis = initState.isEmailRecallDetailListVis;
  @observable recallStatus = initState.recallStatus;
  @observable recallRule = initState.recallRule;
  @observable recallNoTrace = initState.recallNoTrace;
  @observable classificationRight = initState.classificationRight; // 密级查看权限

  constructor(id) {
    this.id = id;
    this.readListTableStore = new TableStore();
  }

  @action
  setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
    return this;
  }

  @action
  getSendApartResultList = (mailId) => {
    Api.getSendApartResultList({ mailId })
      .then(action((data) => {
        const { status } = data;
        if (status === '1') {
          this.sendApartResultList = data.sendResultList;
          this.sendStatus = data.sendStatus;
        } else {
          message.error(data.msg ? data.msg : getLabel(384415, '获取分别发送结果列表错误'));
        }
      }));
  };


  @action
  getEmailRecallDetailList = (mailId) => {
    Api.getEmailRecallDetailList({ mailId })
      .then(action((data) => {
        const { status } = data;
        if (status === '1') {
          this.emailRecallDetailList = data.emailRecallDetailList;
          this.recallStatus = data.recallStatus;
          this.recallRule = data.recallRule;
          this.recallNoTrace = data.recallNoTrace;
        } else {
          message.error(data.msg ? data.msg : getLabel(505790, '获取撤回结果列表错误'));
        }
      }));
  };

  @action
  emailCommonOperation = (params) => {
    opsApi.commonOperation(params)
      .then((res) => {
        try {
          if (res.flag === true && params.operateType === 'receiveNeedReceipt') {
            message.success(getLabel(384416, '回复成功！'));
          } else if (res.flag === false) {
            message.error(res.msg);
          } else {
          }
        } catch (e) {
          message.error(e);
        }
      });
  };

  @action
  getRight = async (params) => {
    this.rightLoading = true;
    const res = await Api.getRight(params);
    try {
      if (res.status === '1') {
        this.right = res.viewRight;
        this.classificationRight = res.classificationRight;
      } else {
        message.error(res.msg);
      }
    } catch (e) {
      message.error(e);
    }
    this.rightLoading = false;
    return res;
  }

  @action
  getBaseInfo = (params, callback) => {
    this.baseLoading = true;
    this.loading = true;
    const fn = async (p) => {
      const res = await Api.getBase(p);
      try {
        if (res.status === '1') {
		  this.baseInfo = res.viewBean;
          this.isSimple = res.viewBean.showmode;
          callback && callback(res.viewBean);
        } else {
          message.error(res.msg);
        }
      } catch (e) {
        message.error(e);
      }
      this.baseLoading = false;
      this.loading = false;
    };
    fn(params);
    return this;
  }

  @action
  getDropDown = (params) => {
    this.dropdownLoding = true;
    const fn = async (p) => {
      const res = await Api.getDropDown(p);
      try {
        if (res.status === '1') {
          this.dropDown = res.viewPageTopDropDown;
        } else {
          message.error(res.msg);
        }
      } catch (e) {
        message.error(e);
      }
      this.dropdownLoding = false;
    };
    fn(params);
    return this;
  }

  @action
  getContent = (params) => {
    this.contentLoading = true;
    this.loading = true;
    const fn = async (p) => {
      const res = await Api.getContent(p);
      try {
        if (res.status === '1') {
          this.content = Base64.decode(res.mailContent);
          this.content = this.content.replace(/\/spa\/workflow\/forwardMobileForm.html/g, '/workflow/request/ViewRequestForwardSPA.jsp'); // 流程地址打开替换
        } else {
          message.error(res.msg);
        }
      } catch (e) {
        message.error(e);
      }
      this.contentLoading = false;
      this.loading = false;
    };
    fn(params);
    return this;
  }

  @action
  getReadCount = (params) => {
    this.readListLoading = true;
    const fn = async (p) => {
      const res = await Api.getReadCount(p);
      try {
        if (res.status === '1') {
          this.readListCount = res.topList;
        } else {
          message.error(res.msg);
        }
      } catch (e) {
        message.error(e);
      }
      this.readListLoading = false;
    };
    fn(params);
    return this;
  }

  @action
  getReadList = (params) => {
    this.readListLoading = true;
    const fn = async (p) => {
      const res = await Api.getReadList(p);
      try {
        if (res.status === '1') {
          this.readListTableStore.getDatas(res.sessionkey, params.current || 1);
        } else {
          message.error(res.msg);
        }
      } catch (e) {
        message.error(e);
      }
      this.readListLoading = false;
    };
    fn(params);
    return this;
  }

  @action
  doFastReply = async (params) => {
    this.fastReplyLoading = true;
    const res = await Api.doFastReply(params);
    try {
      if (res.status === '1' && (res.api_status && res.api_status === 'true')) {
        message.success(getLabel(384416, '回复成功！'));
      } else {
        message.error(res.msg);
      }
    } catch (e) {
      message.error(e);
    }
    this.fastReplyLoading = false;
    return res;
  }
}
